<?php
	class crud_flagfield {
		function getAllFlagField(){
			$start	= (isset($_REQUEST['start']))? $_REQUEST['start'] : 0;
			$limit	= (isset($_REQUEST['limit']))? $_REQUEST['limit'] : 10;
			$sql = "select 
							fc.ID,
							fc.FLAG_FIELD_ID,
							DATE_FORMAT(fc.PERIOD,'%d/%m/%Y')as PERIOD,
							fc.GOLF_COURSE_ID,
							gc.NAME as NAME_GOLF_COURSE,
							ff.NAMA as NAMA_FLAG
					from flag_course fc
					inner join flag_field ff on ff.ID = fc.FLAG_FIELD_ID
					inner join golf_course gc on gc.ID = fc.GOLF_COURSE_ID where 1=1 ";
			$sqlCount = "select 
							count(*) as total
					from flag_course fc
					inner join flag_field ff on ff.ID = fc.FLAG_FIELD_ID
					inner join golf_course gc on gc.ID = fc.GOLF_COURSE_ID where 1=1 ";
			if (isset($_GET['query'])){
				$sql .= "and (
							gc.NAME like '%".$_GET['query']."%' or
							ff.NAMA like '%".$_GET['query']."%' 
						)";
				$sqlCount .= "and (
							gc.NAME like '%".$_GET['query']."%' or
							ff.NAMA like '%".$_GET['query']."%' 
						)";
			}
			$sql .= " order by gc.NAME LIMIT ".$limit." OFFSET ".$start;
			// echo $sql;
			$resultSql = Yii::app()->db->createCommand($sql)->queryAll();
			$resultCount = Yii::app()->db->createCommand($sqlCount)->queryAll();
			echo '{success:true,results: '.$resultCount[0]['total'].',rows:'.json_encode($resultSql).'}';
		}
		
		function insert(){
			$model = new FlagCourse;
			$transaction = Yii::app()->db->beginTransaction();
			try {
				$model->FLAG_FIELD_ID = $_POST['id_flag-inputEl'];
				$model->PERIOD = Yii::app()->helper->dateFormating($_POST['period-inputEl']);
				$model->GOLF_COURSE_ID = $_POST['id_golfcourse-inputEl'];
				$model->save();
				$transaction->commit();
				$feedback = '{"feedback":[{"message":"Insert Sukses","code":"1","ID":"'.$model->ID.'"}]}';
			} catch (Exception $e) {
				// echo $e;
				$transaction->rollback();
				$feedback = '{"feedback":[{"message":"Insert Gagal","code":"0"}]}';
			}
			echo $feedback;
		}
		
		function update(){
			$model = FlagCourse::model()->findByPk($_POST['id-inputEl']);
			$transaction = Yii::app()->db->beginTransaction();
			try {
				$model->FLAG_FIELD_ID = $_POST['id_flag-inputEl'];
				$model->PERIOD = Yii::app()->helper->dateFormating($_POST['period-inputEl']);
				$model->GOLF_COURSE_ID = $_POST['id_golfcourse-inputEl'];
				$model->save();
				$transaction->commit();
				$feedback = '{"feedback":[{"message":"Update Sukses","code":"1","ID":"'.$model->ID.'"}]}';
			} catch (Exception $e) {
				// echo $e;
				$transaction->rollback();
				$feedback = '{"feedback":[{"message":"Update Gagal","code":"0"}]}';
			}
			echo $feedback;
		}
		
		function delete(){
			$model = FlagCourse::model()->findByPk($_POST['id']);
			$transaction = Yii::app()->db->beginTransaction();
			try {
				$model->delete();
				$transaction->commit();
				$feedback = '{"feedback":[{"message":"Flag Field Setting Berhasil Dihapus","code":"1","ID":"'.$model->ID.'"}]}';
			} catch (Exception $e) {
				// echo $e;
				$transaction->rollback();
				$feedback = '{"feedback":[{"message":"Flag Field Setting Gagal Dihapus","code":"0"}]}';
			}
			echo $feedback;
		}
	}
?>